package com.ysd.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.ysd.dao.impl.NewsTableDaoImpl;
import com.ysd.dao.impl.ProductclassifiCationTableDaoImpl;
import com.ysd.entity.NewsTable;
import com.ysd.entity.ProductclassifiCationTable;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;

public class AdminNewstableCategoryServlet extends BaseServlet {
	public void getnewstable(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		NewsTableDaoImpl pcDao = new NewsTableDaoImpl();//创建实现类对象
		int page = Integer.parseInt(request.getParameter("page"));
		int size = Integer.parseInt(request.getParameter("size"));
		List<NewsTable> pclist= pcDao.getNewsTableBypage(page, size);//查询所有分类
		response.setCharacterEncoding("UTF-8");//编码格式
		PrintWriter out = response.getWriter();//out对象
		Gson gson = new Gson();
		String res = gson.toJson(pclist);//将pclist装华为json字符串
		out.print(res);//响应数据
		out.flush();
		out.close();
	}
	/**
	 * 
	 * */
	public void getnewstableBySize(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		NewsTableDaoImpl pcDao = new NewsTableDaoImpl();//创建实现类对象
		List<NewsTable> pclist= pcDao.getNewsTable();//查询所有分类
		response.setCharacterEncoding("UTF-8");//编码格式
		PrintWriter out = response.getWriter();//out对象
		Gson gson = new Gson();
		String res = gson.toJson(pclist.size());//将pclist装华为json字符串
		out.print(res);//响应数据
		out.flush();
		out.close();
	}
	public void delnewstable(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int cId = Integer.parseInt(request.getParameter("newsConsultationId"));
		NewsTableDaoImpl pcDao = new NewsTableDaoImpl();
		int n= pcDao.deletenewstable(cId);
		response.setCharacterEncoding("UTF-8");//编码格式
		PrintWriter out = response.getWriter();//out对象
		if (n>0) {
			out.print(Result.toClient(true, "删除成功",""));
		} else {
			out.print(Result.toClient(false, "删除失败",""));
		}
		out.flush();
		out.close();
	}
	public void addnew(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");//编码格式
		PrintWriter out = response.getWriter();//out对象
		NewsTableDaoImpl pcDao = new NewsTableDaoImpl();

		String classification = request.getParameter("asdf");
		//接受前台传来的参数
		if (classification!=null&&!classification.equals("")) {
			List<NewsTable> pcList = pcDao.getnews(classification);
			if (pcList.size()==0) {
				NewsTable pc = new NewsTable(0, classification);
				//调用构造方法，创建餐品分类的对象
				System.out.println(pc);//打印
				//创建实现类对象
				int n= pcDao.addnews(pc);
				// 调用实现类的构造方法执行sql语句返回受影响行数
				if (n>0) {
					out.print(Result.toClient(true, "成功",""));
					//大于0成功
				} else {
					out.print(Result.toClient(false, "删除失败",""));
					//否则失败
				}
			} else {
				out.print(Result.toClient(false, "分类名已存在",""));
			}
		}
		
		out.flush();
		//清空
		out.close();
		//关闭
	}
	public void getnew(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		NewsTableDaoImpl pcDao = new NewsTableDaoImpl(); 
		int cateId = Integer.parseInt(request.getParameter("cId"));
		NewsTable pc = pcDao.getnewsbyid(cateId); 
		Gson gson = new Gson();
		String res = gson.toJson(pc);
		out.print(res);
		
		out.flush();
		//清空
		out.close();
		//关闭
	}
	public void updatenewstable(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		NewsTableDaoImpl pcDao = new NewsTableDaoImpl();
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		int newsConsultationId = Integer.parseInt(request.getParameter("newsConsultationId"));//接受前台传来的参数
		String classification = request.getParameter("classification");
		NewsTable pc = new NewsTable(newsConsultationId,classification); 
		int n = pcDao.updateNews(pc); 
		if (n>0) {
			out.print(Result.toClient(true, "修改成功",""));
		} else {
			out.print(Result.toClient(false, "修改失败",""));
		}
		out.flush();
		out.close();
	}
}
